﻿Public Class LISTIDANDPRODUCT
    Private connect As CONDB = CONDB.NewConnection()
    Private sC As FILTERCLASS
    Dim s As FILTERCLASSGRID
    Dim PROVIDER_Check As New PROVIDERSCLASS

    Private Sub LISTIDANDPRODUCT_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        SelectDatagridMASPRODUCT()
        'SearchCCODE()
        'SearchCCODE2()
        SearchPRDCATTxt()
        Searchdata()
        Searchdata2()

        DesignForm(Me)
        DesignGrid(MASPRODUCTGrid)

        PROVIDER_Check.GETHOSEM(CInt(main.txtUserRequest.Text.Trim), Convert.ToString(Me.Tag))
    End Sub

    Private Sub SelectDatagridMASPRODUCT()
        Dim objSelectDataMASPRODUCT As New MASPRODUCT
        Dim MASPRODUCT As DataTable
        MASPRODUCT = connect.GetTable(objSelectDataMASPRODUCT.SelectMasproduct_)
        MASPRODUCTGrid.PrimaryGrid.DataSource = MASPRODUCT
    End Sub

    Private Sub Searchdata()
        Dim sql As String
        sql = "SELECT `ccode`,`name` FROM myfriendsdb.finchcode;"
        Dim zx As New FILTERCLASSGRID(MASPRODUCTGrid, sql, "รหัส,รหัสรายได้", "100,300", "0,1", "0,1")
        zx.SetShowBorder = True
        zx.SetShowInColumnName = "รหัสรายได้"
        'zx.SetShowInColumnName = "ผู้บันทึก"
    End Sub

    Private Sub Searchdata2()
        Dim sql As String
        sql = "SELECT `rcode`,`name` FROM myfriendsdb.finrecode WHERE `STATUS` = 1;"
        s = New FILTERCLASSGRID(MASPRODUCTGrid, sql, "รหัส,รหัสรายรับทางการเงิน", "100,300", "0,1", "0,1")
        s.SetShowBorder = True
        s.SetShowInColumnName = "รหัสรายรับทางการเงิน"
        'zx.SetShowInColumnName = "ผู้บันทึก"
    End Sub

    Private Sub SearchPRDCATTxt()
        Dim sql As String
        sql = "SELECT `prdcat`,`catname` FROM masprdcats"
        sC = New FILTERCLASS(PRDCATTxt, sql, "รหัส,ชนิดสินค้า", "80,203", "0,1", "0,1")
        sC.SetTagIndex = 0 ' settag
        sC.SetTextIndex = 1 'settext
        sC.SetShowBorder = True
    End Sub

    Private Sub check()
        Dim cx As New DevComponents.DotNetBar.SuperGrid.GridCell
        Try
            For n As Integer = 0 To MASPRODUCTGrid.PrimaryGrid.Rows.Count - 1
                cx = CType(MASPRODUCTGrid.PrimaryGrid.Rows(n), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก")
                If CType(MASPRODUCTGrid.PrimaryGrid.Rows(n), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Value.ToString.Trim <> "" And CType(MASPRODUCTGrid.PrimaryGrid.Rows(n), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Value.ToString.Trim <> "" Then
                    cx.Value = "1"
                Else
                    cx.Value = "0"
                End If
            Next n
            MASPRODUCTGrid.PrimaryGrid.SetSort({MASPRODUCTGrid.PrimaryGrid.Columns(4), MASPRODUCTGrid.PrimaryGrid.Columns(5)}, {DevComponents.DotNetBar.SuperGrid.SortDirection.Ascending, DevComponents.DotNetBar.SuperGrid.SortDirection.Descending})

        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub PRDCATTxt_Click(sender As Object, e As EventArgs) Handles PRDCATTxt.Click
        PRDCATTxt.Text = ""
        PRDCATTxt.Tag = ""
    End Sub

    Private Sub SwitchButton1_ValueChanged(sender As Object, e As EventArgs) Handles SwitchButton1.ValueChanged
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                Dim editStat As Integer
                editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
                If editStat = 1 Then

                    Dim Sql As String = ""
                    If SwitchButton1.Value = True Then 'กรณีเลือก
                        'If Convert.ToString(CCODETxt2.Tag).Trim = "" Then
                        '    MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน")
                        '    CCODETxt2.Focus()
                        '    Return
                        'End If
                        'If Convert.ToString(RCODETxt2.Tag).Trim = "" Then
                        '    MsgBox("กรุณากรอกข้อมูลให้ครบถ้วน")
                        '    RCODETxt2.Focus()
                        '    Return
                        'End If

                        For ton As Integer = 0 To MASPRODUCTGrid.PrimaryGrid.Rows.Count - 1
                            CType(MASPRODUCTGrid.PrimaryGrid.Rows(ton), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value = 1
                            Sql += "UPDATE masproduct SET "
                            If CType(MASPRODUCTGrid.PrimaryGrid.Rows(0), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value = 1 Then
                                Sql += "`ccode` = '" & CType(MASPRODUCTGrid.PrimaryGrid.Rows(ton), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value & "' ,"
                                Sql += "`rcode` = '" & CType(MASPRODUCTGrid.PrimaryGrid.Rows(ton), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value & "' "
                            End If
                            Sql += "WHERE `prdcode` = '" & CType(MASPRODUCTGrid.PrimaryGrid.Rows(ton), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("prdcode").Value.ToString.Trim & "' ;"
                        Next
                    Else 'กรณีไม่เลือก
                        For ton As Integer = 0 To MASPRODUCTGrid.PrimaryGrid.Rows.Count - 1
                            CType(MASPRODUCTGrid.PrimaryGrid.Rows(ton), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value = 0
                            Sql += "UPDATE masproduct SET "
                            Sql += "`ccode` = Null, "
                            Sql += "`rcode` = Null "
                            Sql += "WHERE `prdcode` = '" & CType(MASPRODUCTGrid.PrimaryGrid.Rows(ton), DevComponents.DotNetBar.SuperGrid.GridRow).Cells("prdcode").Value.ToString.Trim & "' ;"
                        Next
                    End If
                    'Excecute Database
                    connect = CONDB.NewConnection()
                    connect.BeginTrans()
                    Try
                        connect.ExecuteNonQuery(Sql)
                        connect.CommitTrans()
                    Catch ex As Exception
                        connect.RollbackTrans()
                        MsgBox(ex.ToString)
                    Finally
                        connect.Dispose()
                    End Try
                    Dim dt As New DataTable
                    Sql = "SELECT `prdcode`,`prdname` as 'รายการ',masprdcats.`prdcat`,masprdcats.`catname` as 'ชนิดสินค้า',finchcode.`ccode`,finchcode.`name` as 'รหัสรายได้',finrecode.`rcode`,finrecode.`name` as 'รหัสรายรับทางการเงิน',masproduct.`status` as 'สถานะการเปิดใช้งาน' FROM masproduct LEFT JOIN (SELECT `prdcat`,`catname` FROM masprdcats) as masprdcats on masproduct.`prdcat` = masprdcats.`prdcat` LEFT JOIN (SELECT `ccode`,`name` FROM finchcode WHERE `status` = 1) as finchcode on masproduct.`ccode` = finchcode.`ccode` LEFT JOIN (SELECT `rcode`,`name` FROm finrecode WHERE `status` = 1) as finrecode on masproduct.`rcode` = finrecode.`rcode` WHERE masprdcats.`prdcat` = '" & Convert.ToString(PRDCATTxt.Tag).Trim & "'"
                    dt = connect.GetTable(Sql)
                    MASPRODUCTGrid.PrimaryGrid.DataSource = dt
                End If
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub editMASPRODUCT_Click(sender As Object, e As EventArgs)
        'Dim editStat As Integer
        'editStat = MsgBox("ท่านต้องการแก้ไขข้อมูลส่วนดังกล่าวใช่หรือไม่", MsgBoxStyle.OkCancel)
        'If editStat = 1 Then
        '    Dim objeditData As New MASPRODUCT
        '    objeditData.PRDCAT_ = Convert.ToString(PRDCATTxt.Tag).Trim
        '    objeditData.CCODE_ = Convert.ToString(CCODETxt2.Tag).Trim
        '    objeditData.RCODE_ = Convert.ToString(RCODETxt2.Tag).Trim
        '    objeditData.STATUS_ = MASPRODUCTstatuscheck.Checked
        '    objeditData.editMasproductdata()

        '    SelectDatagridFinrecode()

        '    'cleardata()

        'End If


    End Sub

    Private Sub PRDCATTxt_TextChanged(sender As Object, e As EventArgs) Handles PRDCATTxt.TextChanged
        Dim sql As String
        Dim dt As New DataTable
        If PRDCATTxt.Text.Length > 0 Then
            sql = "SELECT `prdcode`,`prdname` as 'รายการ',masprdcats.`prdcat`,masprdcats.`catname` as 'ชนิดสินค้า',finchcode.`ccode`,finchcode.`name` as 'รหัสรายได้',finrecode.`rcode`,finrecode.`name` as 'รหัสรายรับทางการเงิน',masproduct.`status` as 'สถานะการเปิดใช้งาน' FROM masproduct LEFT JOIN (SELECT `prdcat`,`catname` FROM masprdcats) as masprdcats on masproduct.`prdcat` = masprdcats.`prdcat` LEFT JOIN (SELECT `ccode`,`name` FROM finchcode WHERE `status` = 1) as finchcode on masproduct.`ccode` = finchcode.`ccode` LEFT JOIN (SELECT `rcode`,`name` FROm finrecode WHERE `status` = 1) as finrecode on masproduct.`rcode` = finrecode.`rcode` WHERE masprdcats.`prdcat` = '" & Convert.ToString(PRDCATTxt.Tag).Trim & "'"
            dt = connect.GetTable(sql)
            MASPRODUCTGrid.PrimaryGrid.DataSource = dt
        Else
            sql = "SELECT `prdcode`,`prdname` as 'รายการ',masprdcats.`prdcat`,masprdcats.`catname` as 'ชนิดสินค้า',finchcode.`ccode`,finchcode.`name` as 'รหัสรายได้',finrecode.`rcode`,finrecode.`name` as 'รหัสรายรับทางการเงิน',masproduct.`status` as 'สถานะการเปิดใช้งาน' FROM masproduct LEFT JOIN (SELECT `prdcat`,`catname` FROM masprdcats) as masprdcats on masproduct.`prdcat` = masprdcats.`prdcat` LEFT JOIN (SELECT `ccode`,`name` FROM finchcode WHERE `status` = 1) as finchcode on masproduct.`ccode` = finchcode.`ccode` LEFT JOIN (SELECT `rcode`,`name` FROm finrecode WHERE `status` = 1) as finrecode on masproduct.`rcode` = finrecode.`rcode`"
            dt = connect.GetTable(sql)
            MASPRODUCTGrid.PrimaryGrid.DataSource = dt
        End If

        If PRDCATTxt.Text.Length > 0 Then
            SwitchButton1.Enabled = True
        End If
    End Sub

    Private Sub MASPRODUCTGrid_CellClick(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellClickEventArgs) Handles MASPRODUCTGrid.CellClick
        connect = CONDB.NewConnection()
        If main.PROVIDERPOLICIES.checklogin_user_ = True Then
            If PROVIDER_Check.PUPD_ = True Then
                If e.GridCell.GridColumn.Name = "เลือก" Then
                    Try
                        'If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value.ToString.Trim = "True" Or CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value.ToString.Trim = "1" Then
                        '    CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value = 0
                        'Else
                        '    CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value = 1
                        'End If
                        Dim Sql As String
                        connect.BeginTrans()
                        Sql = "UPDATE masproduct SET "
                        'If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value.ToString.Trim = "0" Then
                        '    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value.ToString.Trim = "" Then
                        '        Sql += "`ccode` = Null, "
                        '    Else
                        '        Sql += "`ccode` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value.ToString.Trim & "' ,"
                        '    End If
                        '    If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value.ToString.Trim = "" Then
                        '        Sql += "`rcode` = Null "
                        '    Else
                        '        Sql += "`rcode` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value.ToString.Trim & "' "
                        '    End If
                        If Convert.ToBoolean(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value()) = True Then
                            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value.ToString.Trim = "" Then
                                Sql += "`ccode` = Null, "
                            Else
                                Sql += "`ccode` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value.ToString.Trim & "' ,"
                            End If
                            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value.ToString.Trim = "" Then
                                Sql += "`rcode` = Null "
                            Else
                                Sql += "`rcode` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value.ToString.Trim & "' "
                            End If
                        Else
                            Sql += "`ccode` = Null, "
                            Sql += "`rcode` = Null "
                        End If
                        Sql += "WHERE `prdcode` = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("prdcode").Value.ToString.Trim & "' ;"
                        connect.ExecuteNonQuery(Sql)
                        connect.CommitTrans()
                    Catch ex As Exception
                        connect.RollbackTrans()
                        MsgBox(ex.ToString)
                    End Try
                ElseIf e.GridCell.GridColumn.Name = "รหัสรายรับทางการเงิน" Then
                    s.SetSql = "SELECT `rcode`,`name` FROM myfriendsdb.finrecode WHERE `status` = 1 AND ccode = '" & CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value & "';"
                End If
                connect.Dispose()
            Else
                MsgBox(PROVIDER_Check.PUPDMSG_)
            End If
        Else
            TOASTCLASS.gloadToastMSG(Me, main.PROVIDERPOLICIES.LOGINUSERMSG_)
            main.txtUserRequest.Focus()
        End If
    End Sub

    Private Sub MASPRODUCTGrid_DataBindingComplete(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridDataBindingCompleteEventArgs) Handles MASPRODUCTGrid.DataBindingComplete
        check()
    End Sub

    
    Private Sub MASPRODUCTGrid_CellValueChanged(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellValueChangedEventArgs) Handles MASPRODUCTGrid.CellValueChanged
        If e.GridCell.GridColumn.Name = "รหัสรายได้" Then
            CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Value = ""
        End If

        If e.GridCell.GridColumn.Name = "รหัสรายได้" Or e.GridCell.GridColumn.Name = "รหัสรายรับทางการเงิน" Then
            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Tag Is DBNull.Value Then
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value = DBNull.Value
            ElseIf CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Tag Is Nothing Then
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value = DBNull.Value
            ElseIf Convert.ToString(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Tag) = "" Then
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value = DBNull.Value
            Else
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("ccode").Value = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายได้").Tag
            End If

            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Tag Is DBNull.Value Then
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value = DBNull.Value
            ElseIf CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Tag Is Nothing Then
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value = DBNull.Value
            ElseIf Convert.ToString(CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Tag).Trim = "" Then
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value = DBNull.Value
            Else
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("rcode").Value = CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("รหัสรายรับทางการเงิน").Tag
            End If
        End If
    End Sub

    Private Sub MASPRODUCTGrid_CellMouseDown(sender As Object, e As DevComponents.DotNetBar.SuperGrid.GridCellMouseEventArgs) Handles MASPRODUCTGrid.CellMouseDown
        If e.GridCell.GridColumn.Name = "เลือก" Then
            If CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value.ToString.Trim = "True" Or CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value.ToString.Trim = "1" Then
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value = 0
            Else
                CType(e.GridPanel.ActiveRow, DevComponents.DotNetBar.SuperGrid.GridRow).Cells("เลือก").Value = 1
            End If
        End If
    End Sub

   
End Class